﻿namespace com.oakwoodsys.ConsultantCareDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.oakwoodsys.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = ConsultantCareDataContext;

    using ReferenceObject = Consultant;

    public class ConsultantDao : BaseDao
    {
        #region Fields

        public const string ListName = "consultants";

        #endregion Fields

        #region Constructors

        public ConsultantDao( )
            : base(ListName)
        {
            CurrentWebUrl = SPContext.Current.Web.Url;
        }

        public ConsultantDao( string currentWebUrl )
            : base(ListName)
        {
            CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public ReferenceObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.Id.HasValue && o.Id.Value == id
                                      select o ).SingleOrDefault( );

                setAssociatedRecruiter( v );
                return v;
            }
        }

        public ReferenceObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == id
                                      select o ).SingleOrDefault( );

                setAssociatedRecruiter( v );
                return v;
            }
        }

        public List<ReferenceObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle ).ToList( );

                foreach ( ReferenceObject o in l )
                    setAssociatedRecruiter( o );
                return l;
            }
        }

        public List<ReferenceObject> GetAllActive( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.Active.HasValue && o.Active.Value == true &&
                                            ( o.Type.HasValue && o.Type.Value == Type.Consultant ||
                                            o.Type.HasValue && o.Type.Value == Type.Permanent )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );

                foreach ( ReferenceObject o in v )
                    setAssociatedRecruiter( o );
                return v;
            }
        }

        public List<ReferenceObject> GetAllActiveConsultant( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.Active.HasValue && o.Active.Value == true &&
                                            o.Type.HasValue && o.Type.Value == Type.Consultant
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );

                foreach ( ReferenceObject o in v )
                    setAssociatedRecruiter( o );
                return v;
            }
        }

        public List<ReferenceObject> GetAllActivePermanent( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.Active.HasValue && o.Active.Value == true &&
                                            o.Type.HasValue && o.Type.Value == Type.Permanent
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );

                foreach ( ReferenceObject o in v )
                    setAssociatedRecruiter( o );
                return v;
            }
        }

        public ReferenceObject Update( ReferenceObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == value.UniqueId
                                      select o ).SingleOrDefault( );
                if ( v != null )
                {
                    v.Active = value.Active;
                    v.ClientName = value.ClientName;
                    v.DOB = value.DOB;
                    v.EmailAddress = value.EmailAddress;
                    v.Note = value.Note;
                    v.RecruiterId = value.RecruiterId;
                    v.StartDate = value.StartDate;
                    v.TelMobile = value.TelMobile;
                    v.TelOther = value.TelOther;
                    v.Title = value.Title;
                    v.Type = value.Type;
                    // commit changes
                    c.SubmitChanges( );
                    return v;
                }
                else
                    return null;
            }
        }

        void setAssociatedRecruiter( ReferenceObject consultant )
        {
            if ( !consultant.RecruiterId.HasValue )
                consultant.AssociatedRecruiter = null;
            RecruiterDao dao = new RecruiterDao( CurrentWebUrl );
            consultant.AssociatedRecruiter = dao.GetByRecruiter( consultant.RecruiterId.Value );
        }

        #endregion Methods
    }
}